<template>
  <el-card>
    <el-form :inline="true" class="form">
      <el-form-item label="职位搜索">
        <el-input
          placeholder="请你输入搜索职位关键字"
          v-model="keyword"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          size="default"
          :disabled="keyword ? false : true"
          @click="search"
          >搜索</el-button
        >
        <el-button type="primary" size="default" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <el-card style="margin: 10px 0px">
    <el-button type="primary" size="default" icon="Plus" @click="addRole"
      >添加职位</el-button
    >
    <el-table border style="margin: 10px 0px" :data="allRole">
      <el-table-column type="index" align="center" label="#"></el-table-column>
      <el-table-column label="ID" align="center" prop="id"></el-table-column>
      <el-table-column
        label="职位名称"
        align="center"
        show-overflow-tooltip
        prop="roleName"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column label="操作" width="280px" align="center">
        <!-- row:已有的职位对象 -->
        <template #="{ row, $index }">
          <el-button
            type="primary"
            size="small"
            icon="Plus"
            @click="setPermisstion(row)"
            >分配权限</el-button
          >
          <el-button
            type="primary"
            size="small"
            icon="Edit"
            @click="updateRole(row)"
            >编辑</el-button
          >
          <el-popconfirm
            :title="`你确定要删除${row.roleName}`"
            width="260px"
            @confirm="removeRole(row.id)"
          >
            <template #reference>
              <el-button type="primary" size="small" icon="Delete"
                >删除</el-button
              >
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @current-change="getHasRole"
      @size-change="sizeChange"
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 30]"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
    >
    </el-pagination>
  </el-card>

  <!-- 添加职位与更新已有职位的结构:对话框 -->
  <el-dialog
    v-model="dialogVisite"
    :title="RoleParams.id ? '更新职位' : '添加职位'"
  >
    <el-form :model="RoleParams" :rules="rules" ref="form">
      <el-form-item label="职位名称" prop="roleName">
        <el-input
          placeholder="请你输入职位名称"
          v-model="RoleParams.roleName"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" size="default" @click="dialogVisite = false"
        >取消</el-button
      >
      <el-button type="primary" size="default" @click="save">确定</el-button>
    </template>
  </el-dialog>

  <!-- 抽屉组件:分配职位的菜单权限与按钮的权限 -->
  <el-drawer v-model="drawer">
    <template #header>
      <h4>分配菜单与按钮的权限</h4>
    </template>
    <!-- 树形控件 -->
    <template #default>
      <el-tree
        ref="tree"
        :data="menuArr"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="selectArr"
        :props="defaultProps"
      />
    </template>

    <template #footer>
      <div style="flex: auto">
        <el-button @click="drawer = false">取消</el-button>
        <el-button type="primary" @click="handler">确定</el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive, nextTick } from "vue";
// 请求方法
import {
  reqAddOrUpdateRole,
  reqAllRoleList,
  reqAllMenuList,
  reqSertPermisstion,
  reqRemoveRole,
} from "@/api/acl/role";
//TS类型
import type {
  RoleResponseData,
  Records,
  MenuResponseData,
  MenuList,
} from "@/api/acl/role/type";
// 引入仓库
import userLayOutSettingStore from "@/store/modules/setting";
import { RoleData } from "@/api/acl/role/type";
import { ElMessage } from "element-plus";
let settingStore = userLayOutSettingStore();

// 当前页面

let pageNo = ref<number>(1);
// 一页展示几条数据
let pageSize = ref<number>(10);
// 搜索职位的关键词
let keyword = ref<string>("");
// 存储全部已有的职位
let allRole = ref<Records>([]);
// 职位总个数
let total = ref<number>(0);
// 控制对话框的显示与隐藏
let dialogVisite = ref<boolean>(false);
// 收集新增岗位的数据
let RoleParams = reactive<RoleData>({
  roleName: "",
});
// 获取form组件实列
let form = ref<any>();
// 控制抽屉显示与隐藏
let drawer = ref<boolean>(false);
// 定义数组存储用户权限的数据
let menuArr = ref<MenuList>([]);
// 存储勾选的节点ID(四级节点)
let selectArr = ref<number[]>([]);

// 获取树形控件的组件实列
let tree = ref<any>();

// 树形控件
const defaultProps = {
  children: "children",
  label: "name",
};

// 组件挂载完毕
onMounted(() => {
  getHasRole();
});

// 列表请求
const getHasRole = async (pager = 1) => {
  // 修改当前页面
  pageNo.value = pager;
  let result: RoleResponseData = await reqAllRoleList(
    pageNo.value,
    pageSize.value,
    keyword.value,
  );
  if (result.code == 200) {
    total.value = result.data.total;
    allRole.value = result.data.records;
  }
};

const sizeChange = () => {
  getHasRole();
};

// 搜索按钮的回调
const search = () => {
  getHasRole();
  keyword.value = "";
};

// 重置按钮的回调
const reset = () => {
  settingStore.refsh = !settingStore.refsh;
};

// 添加职位按钮的回调
const addRole = () => {
  // 对话框显示
  dialogVisite.value = true;
  // 清空数据
  Object.assign(RoleParams, {
    id: 0,
    roleName: "",
  });
  // 清空上一次表单校验错误结果
  nextTick(() => {
    form.value.clearValidate("roleName");
  });
};

// 更新已有的职位按钮的回调
const updateRole = (row: RoleData) => {
  // 对话框显示
  dialogVisite.value = true;
  // 存储已有的职位---带有ID的
  Object.assign(RoleParams, row);
  // 清空上一次表单校验错误结果
  nextTick(() => {
    form.value.clearValidate("roleName");
  });
};

// 自定义校验规则的回调
const validatorRoleName = (rule: any, value: any, callBack: any) => {
  if (value.trim().length >= 2) {
    callBack();
  } else {
    callBack(new Error("职位名称最少两位"));
  }
};

// 职位相关的校验规则
const rules = {
  roleName: [{ required: true, trigger: "blur", validator: validatorRoleName }],
};

// 确定按钮的回调
const save = async () => {
  // 表单校验结果,结果通过在发请求,结果没有通过不应该在发生请求
  await form.value.validate();
  // 添加职位 | 更新职位的请求
  let result: any = await reqAddOrUpdateRole(RoleParams);
  if (result.code == 200) {
    // 提示文字
    ElMessage({
      type: "success",
      message: RoleParams.id ? "更新成功" : "添加成功",
    });
    // 对话框隐藏
    dialogVisite.value = false;
    // 获取全部的已有职位
    getHasRole(RoleParams.id ? pageNo.value : 1);
  }
};

// 按钮权限的分配
const setPermisstion = async (row: RoleData) => {
  // 抽屉显示出来
  drawer.value = true;
  // 收集当前要分类权限的职位的数据
  Object.assign(RoleParams, row);
  // 根据职位获取权限的数据
  let result: MenuResponseData = await reqAllMenuList(RoleParams.id as number);
  if (result.code == 200) {
    menuArr.value = result.data;
    selectArr.value = filterSelectArr(menuArr.value, []);
  }
};

const filterSelectArr = (allData: any, initArr: any) => {
  allData.forEach((item: any) => {
    if (item.select && item.level == 4) {
      initArr.push(item.id);
    }
    if (item.children && item.children.length > 0) {
      filterSelectArr(item.children, initArr);
    }
  });
  return initArr;
};

//确认添加权限
const handler = async () => {
  const roleId = RoleParams.id as number;
  // 选择节点的ID
  let arr = tree.value.getCheckedKeys();
  // 半选的ID
  let arr1 = tree.value.getHalfCheckedKeys();
  let permissionId = arr.concat(arr1);
  // 下发权限
  let result: any = await reqSertPermisstion(roleId, permissionId);
  if (result.code == 200) {
    drawer.value = false;

    // 提示信息
    ElMessage({
      type: "success",
      message: "分配权限成功",
    });
    //页面刷新
    window.location.reload();
  } else {
    drawer.value = false;

    // 提示信息
    ElMessage({
      type: "error",
      message: "分配权限失败",
    });
  }
  console.log(result);
};

// 删除已有的职位
const removeRole = async (id: number) => {
  let result: any = await reqRemoveRole(id);
  if (result.code == 200) {
    ElMessage({
      type: "success",
      message: "删除成功!",
    });

    // 刷新
    getHasRole(allRole.value.length > 1 ? pageNo.value : pageNo.value - 1);
  }
};
</script>

<style scoped lang="scss">
.form {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
}
</style>
